<?php
/**
 * @date 2012-3-7
 * @author GhostWei
 */
class controller_admin_account extends system_user_control {

	public function init() {
		if(!parent::init()) {
			return ;
		}
		$this->_crumbs()->addPage(system_nav_page::create($this->language('My Account'))->setRoute('account/index'));
	}
		
	public function action_index() {
		$this->_crumbs()->addPage(system_nav_page::create($this->language('Review'))->setRoute('account/index'));
		$this->render();
	}
	
	public function action_email() {
		if($this->getRequest()->isPost()) {
			try {
				$email = trim($this->_post('email'));
				$row = $this->user;
				$row->setEmail($email);
				if(!system_passport_instance::emailCheck($email)) {
					throw new system_exception("Email formation Error.");
				}
				$concept = new system_passport_concept();
				if($concept->fetch(array('id != ?' => $row->getId(), 'email = ?' => $email))) {
					throw new system_exception("This Email you Changed has been already Existing.");
				}
				$row->update();
				$this->view->throwMessage = 'Complete';
			} catch (Exception $e) {
				$this->view->throwException = $e;
			}
		}
		$this->_crumbs()->addPage(system_nav_page::create($this->language('Change Email'))->setRoute('account/email'));
		$this->render();
	}
	
	public function action_password() {
		if($this->getRequest()->isPost()) {
			try {
				$p1 = trim($this->_post('password1'));
				$p2 = trim($this->_post('password2'));
				$p3 = trim($this->_post('password'));
				if(md5($p1) != $this->user->getCredential()) {
					throw new system_exception("OLD Password is wrong.");
				}
				if(strlen($p2) < 6) {
					throw new system_exception("NEW Password is too short.");
				}
				if(md5($p2) != md5($p3)) {
					throw new system_exception("NEW Password is not different.");
				}
				$row = $this->user;
				$row->setCredential(md5($p2));
				$row->update();
				$this->view->throwMessage = 'Complete';
			} catch (Exception $e) {
				$this->view->throwException = $e;
			}
		}
		$this->_crumbs()->addPage(system_nav_page::create($this->language('Change Password'))->setRoute('account/password'));
		$this->render();
	}
	
	public function action_setting() {
		if($this->getRequest()->isPost()) {
			try {
				$nickname = trim($this->_post('nickname'));				
				$row = $this->user;
				$row->setNickname($nickname);
				$row->save();
				$this->view->throwMessage = 'Complete';
			} catch (Exception $e) {
				$this->view->throwException = $e;
			}
		}
		$this->_crumbs()->addPage(system_nav_page::create($this->language('General Setting'))->setRoute('account/setting'));
		$this->render();
	}
	
}